package model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Class Message
 */
public class Message {

    public static final int MAX_LENGTH = 140;
    
    //
    // Fields
    //
    
    /**
     * Message id (for next acess)
     */
    private int id;
    /**
     * Message author
     */
    private User author;
    /**
     * The actual message
     */
    private String text;
    /**
     * Message date and time creation
     */
    private Date dateTime;
    /**
     * Message comments
     */
    private ArrayList<Message> comments;

    //
    // Constructors
    //
    
    /**
     * Create a new Message object without id. Must be used when id is
     * generated for the DBMS.
     * @param author Message author
     * @param text The message indeed
     * @param dateTime Date and time when message were posted
     * @param comments Message comments
     * @throws InvalidValueException if any of the parameters are invalid
     */
    public Message(User author, String text, Date dateTime,
            ArrayList<Message> comments) 
            throws InvalidValueException {
        this.id = -1;
        setAuthor(author);
        setText(text);
        if (Calendar.getInstance().before(dateTime))
            throw new InvalidValueException("The message date must be earlier than current date.");
        this.dateTime = dateTime;
        this.comments = comments;
    }
    
    /**
     * Create a new Message object without id and comments. Must be used
     * when id is generated for the DBMS.
     * @param author Message author
     * @param text The message indeed
     * @param dateTime Date and time when message were posted
     * @throws InvalidValueException if any of the parameters are invalid
     */
    public Message(User author, String text, Date dateTime) 
            throws InvalidValueException {
        this(author, text, dateTime, new ArrayList<Message>());
    }
    
    /**
     * Create a new Message object
     * @param id Message id
     * @param author Message author
     * @param text The message indeed
     * @param dateTime Date and time when message were posted
     * @param comments Message comments
     * @throws InvalidValueException if any of the parameters are invalid
     */
    public Message(int id, User author, String text, Date dateTime,
            ArrayList<Message> comments) 
            throws InvalidValueException {
        try {
            setId(id);
        } catch (OperationNotAllowedException ex) {};
        setAuthor(author);
        setText(text);
        if (Calendar.getInstance().before(dateTime))
            throw new InvalidValueException("The message date must be earlier than current date.");
        this.dateTime = dateTime;
        this.comments = comments;
    }
    
    /**
     * Create a new Message object
     * @param id Message id
     * @param author Message author
     * @param text The message indeed
     * @param dateTime Date and time when message were posted
     * @throws InvalidValueException if any of the parameters are invalid
     */
    public Message(int id, User author, String text, Date dateTime) 
            throws InvalidValueException {
        this(id, author, text, dateTime, new ArrayList<Message>());
    }

    //
    // Methods
    //
    
    //
    // Accessor methods
    //

    /**
     * Get the value of id
     * Message id (for next acess)
     * @return the value of id
     */
    public int getId() {
        return id;
    }

    /**
     * Set the value of id. This method must be called only when the object
     * is contructed whitout a valid ID, meening that it was generated by a DBMS
     * @param newVar the new value of author
     * @throws InvalidValueException if newVar is less or equal than 0
     * @throws OperationNotAllowedException if the method were called when the
     * object already had a valid ID
     */
    public void setId(int newVar) throws InvalidValueException, OperationNotAllowedException {
        if (this.id > 0)
            throw new OperationNotAllowedException("ID can't be changed if "
                    + "it already has a valid value");
        if (newVar <= 0)
            throw new InvalidValueException("ID have to be greater than 0");
        id = newVar;
    }

    /**
     * Set the value of author
     * Message author
     * @param newVar the new value of author
     * @throws InvalidValueException if newVar is <tt>null</tt>
     */
    public void setAuthor(User newVar) throws InvalidValueException {
        if (newVar == null)
            throw new InvalidValueException("Author can't be null.");
        author = newVar;
    }

    /**
     * Get the value of author
     * Message author
     * @return the value of author
     */
    public User getAuthor() {
        return author;
    }

    /**
     * Set the value of text
     * The actual message
     * @param newVar the new value of text
     * @throws InvalidValueException if newVar length is greater than 140
     */
    public void setText(String newVar) throws InvalidValueException {
        if (newVar.length() > MAX_LENGTH)
            throw new InvalidValueException("Message length have to be less or equal than 140.");
        text = newVar;
    }

    /**
     * Get the value of text
     * The actual message
     * @return the value of text
     */
    public String getText() {
        return text;
    }

    /**
     * Get the value of dateTime
     * Message date and time creation
     * @return the value of dateTime
     */
    public Date getDateTime() {
        return dateTime;
    }

    public ArrayList<Message> getComments() {
        return comments;
    }

    public void addComment(Message comment) {
        comments.add(comment);
    }

    public void removeComment(Message comment) {
        comments.remove(comment);
    }
    
    //
    // Other methods
    //
}
